package com.liyuhang.controller;

import com.liyuhang.domain.User;
import com.liyuhang.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class UserServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html; charset=utf-8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        User user = new User(username, password);
        UserService userService=new UserService();
        if (userService.login(user)) {
            request.getSession().setAttribute("username", username);
            response.sendRedirect("success.jsp");
        } else {
            request.getRequestDispatcher("error.jsp").forward(request, response);
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
